package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.UserGroupUnlockDAO;
import com.jinwei.qunguanjia.user.dto.UserGroupUnlockDTO;
import com.jinwei.qunguanjia.user.po.UserGroupUnlockPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userGroupUnlockService")
public class UserGroupUnlockServiceImpl implements UserGroupUnlockService {

    @Resource
    private UserGroupUnlockDAO userGroupUnlockDAO;

    @Override
    public String save(UserGroupUnlockDTO userGroupUnlockDTO) {
        UserGroupUnlockPO userGroupUnlockPO = new UserGroupUnlockPO();
        BeanUtils.copyProperties(userGroupUnlockDTO, userGroupUnlockPO);
        String userGroupUnlockId = IdUtils.getWorkerId();
        userGroupUnlockPO.setUnlockId(userGroupUnlockId);
        userGroupUnlockPO.setCreateTime(DateUtils.getDateTime());
        userGroupUnlockDAO.insert(userGroupUnlockPO);
        return userGroupUnlockId;
    }

    @Override
    public void remove(String id) {
        userGroupUnlockDAO.deleteById(id);
    }

    @Override
    public void update(UserGroupUnlockDTO userGroupUnlockDTO) {
        UserGroupUnlockPO userGroupUnlockPO = new UserGroupUnlockPO();
        BeanUtils.copyProperties(userGroupUnlockDTO, userGroupUnlockPO);
        userGroupUnlockPO.setUpdateTime(DateUtils.getDateTime());
        userGroupUnlockDAO.updateById(userGroupUnlockPO);
    }

    @Override
    public UserGroupUnlockDTO get(String id) {
        UserGroupUnlockPO userGroupUnlockPO = userGroupUnlockDAO.selectById(id);
        if (null == userGroupUnlockPO) {
            return null;
        }
        UserGroupUnlockDTO userGroupUnlockDTO = new UserGroupUnlockDTO();
        BeanUtils.copyProperties(userGroupUnlockPO, userGroupUnlockDTO);
        return userGroupUnlockDTO;
    }

    @Override
    public UserGroupUnlockDTO get(UserGroupUnlockDTO userGroupUnlockDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userGroupUnlockDTO);
        // 拼接查询条件
        QueryWrapper<UserGroupUnlockPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        UserGroupUnlockPO userGroupUnlockPO = userGroupUnlockDAO.selectOne(queryWrapper);
        if (null == userGroupUnlockPO) {
            return null;
        }
        BeanUtils.copyProperties(userGroupUnlockPO, userGroupUnlockDTO);
        return userGroupUnlockDTO;
    }

    @Override
    public Long count(UserGroupUnlockDTO userGroupUnlockDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userGroupUnlockDTO);
        // 拼接查询条件
        QueryWrapper<UserGroupUnlockPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return userGroupUnlockDAO.selectCount(queryWrapper);
    }

    @Override
    public List<UserGroupUnlockDTO> list(UserGroupUnlockDTO userGroupUnlockDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userGroupUnlockDTO);
        // 拼接查询条件
        QueryWrapper<UserGroupUnlockPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(userGroupUnlockDAO.selectList(queryWrapper), UserGroupUnlockDTO.class);
    }

    @Override
    public PageDataInfo<UserGroupUnlockDTO> listPage(UserGroupUnlockDTO userGroupUnlockDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userGroupUnlockDTO);
        // 拼接查询条件
        QueryWrapper<UserGroupUnlockPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<UserGroupUnlockPO> list = userGroupUnlockDAO.selectList(queryWrapper);
        PageInfo<UserGroupUnlockPO> pageInfo = new PageInfo<>(list);
        PageDataInfo<UserGroupUnlockDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, UserGroupUnlockDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(UserGroupUnlockDTO userGroupUnlockDTO) throws IllegalAccessException {
        String userGroupUnlockId = StringUtils.isNull(userGroupUnlockDTO.getUnlockId()) ? "-1" : userGroupUnlockDTO.getUnlockId();
        UserGroupUnlockDTO info = get(userGroupUnlockDTO);
        if (StringUtils.isNotNull(info) && !info.getUnlockId().equals(userGroupUnlockId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
